package com.atguigu.gmall.pms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.atguigu.core.bean.PageVo;
import com.atguigu.core.bean.QueryCondition;
import com.atguigu.core.bean.Resp;
import com.atguigu.gmall.pms.vo.CategoryEntityVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gmall.pms.entity.CategoryEntity;
import com.atguigu.gmall.pms.service.CategoryService;




/**
 * 商品三级分类
 *
 * @author dhl
 * @email 1558016038@qq.com
 * @date 2020-03-21 16:41:01
 */
@Api(tags = "商品三级分类 管理")
@RestController
@RequestMapping("pms/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询以及分类下面的二级分类及三级分类
     * @param pid
     * @return
     */
    @GetMapping("{pid}")
    public Resp<List<CategoryEntityVO>> querySubCategory(@PathVariable("pid")Long pid){
        List<CategoryEntityVO> categoryEntityList = this.categoryService.querySubCategory(pid);
        return Resp.ok(categoryEntityList);
    }
    //@RequestBody是接收json数据，@RrrequestParam是接收普通数据
    @ApiOperation("根据分类等级或者父id查询分类")
    @GetMapping
    public Resp<List<CategoryEntity>> queryCategory(@RequestParam(value = "level",defaultValue = "0") Integer level,
                                                    @RequestParam(value = "parentCid",required = false) Long parentCid){
        QueryWrapper<CategoryEntity> queryWrapper=new QueryWrapper<>();
        //判断分类的层级是否为0,如果不为0，在拼接查询条件
        // 如果level为0，说明查询所有的级别
        if (level != 0) {
            queryWrapper.eq("cat_level", level);
        }
        // 如果parentCid为null，说明用户没有传该字段，查询所有
        if (parentCid != null){
            queryWrapper.eq("parent_cid", parentCid);
        }

        List<CategoryEntity> list = categoryService.list(queryWrapper);
        return  Resp.ok(list);
    }

  /**
     * 列表
     */
    @ApiOperation("分页查询(排序)")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('pms:category:list')")
    public Resp<PageVo> list(QueryCondition queryCondition) {
        PageVo page = categoryService.queryPage(queryCondition);

        return Resp.ok(page);
    }

    /**
     * 2020 最新
     * 查询所有的分类以及子分类，以树形的方式组装起来
     * @RequestParam Map<String,Object> params
     * @param params
     * @return
     */
    @GetMapping("/list/tree")
    public Resp<List<CategoryEntity>> listTree(){
        List<CategoryEntity> categoryEntityList=categoryService.listWithTree();
        return Resp.ok(categoryEntityList);
    }

    /**
     * 信息
     */
    @ApiOperation("详情查询")
    @GetMapping("/info/{catId}")
    @PreAuthorize("hasAuthority('pms:category:info')")
    public Resp<CategoryEntity> info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return Resp.ok(category);
    }

    /**
     * 保存
     */
    @ApiOperation("保存")
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('pms:category:save')")
    public Resp<Object> save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return Resp.ok(null);
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('pms:category:update')")
    public Resp<Object> update(@RequestBody CategoryEntity category){
		categoryService.updateById(category);

        return Resp.ok(null);
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('pms:category:delete')")
    public Resp<Object> delete(@RequestBody Long[] catIds){
		categoryService.removeByIds(Arrays.asList(catIds));

        return Resp.ok(null);
    }



}
